<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" id="app">
    <div class="layuimini-main">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>{{detail.id?'编辑':'添加'}}</legend>
        </fieldset>
        <form class="layui-form" action="" lay-filter="thisForm">
            <input type="hidden" name="id" :value="detail.id">
            <div class="layui-form-item">
                <label class="layui-form-label">选择渠道</label>
                <div class="layui-input-block">
                    <select name="channel_id" class="layui-select" lay-verify="required" lay-filter="channel_id">
                        <option value="">--请选择--</option>
                        <option :value="item.id" :selected="detail.channel_id==item.id" v-for="item in gameChannelList" lay-ignore="">{{item.name}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">选择包</label>
                <div class="layui-input-block">
                    <div>
                        <input type="radio" title="指定包" name="package_model" :checked="detail.package_model==1" value="1" lay-filter="package_model" />
                        <input type="radio" title="全部" name="package_model" :checked="detail.package_model==2" value="2" lay-filter="package_model" />
                    </div>
                    <div class="list" style="margin-top:20px;">
                        <label v-for="item in packageNoList" style="margin-left:15px;">
                            <input type="checkbox" lay-filter="package_no" name="packageNos[]" :value="item" :checked="packageNoIsChecked(item)" lay-ignore=""/>
                            <span>{{item}}</span>
                        </label>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">选择模式</label>
                <div class="layui-input-block">
                    <select name="model" class="layui-select">
                        <option value="">--请选择--</option>
                        <option value="0" :selected="detail.model===0">显示链接</option>
                        <option value="1" :selected="detail.model===1">消息提示</option>
                        <option value="2" :selected="detail.model===2">全局文案</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">下载链接</label>
                <div class="layui-input-block">
                    <div style="display: flex">
                        <div style="width: 50px;">安卓: </div>
                        <input type="text" name="andriod_url" class="layui-input" :value="detail.andriod_url">
                    </div>
                    <div style="display: flex">
                        <div style="width: 50px;">苹果: </div>
                        <input type="text" name="ios_url" class="layui-input" :value="detail.ios_url">
                    </div>
                    <div style="display: flex">
                        <div style="width: 50px;">PC: </div>
                        <input type="text" name="pc_url" class="layui-input" :value="detail.pc_url">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">提示文本</label>
                <div class="layui-input-block">
                    <input type="text" name="message" class="layui-input" :value="detail.message">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <input type="text" name="comment" class="layui-input" :value="detail.comment">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="submit">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<script>
    var Id = urlParam('id')
    var _this = null;
    var form = null;
    var $;
    var selectedChannelId;
    new Vue({
        el:"#app",
        data:{
            detail:{
                id:null,
                package_model:1,
                package_nos:[],
            },
            gameChannelList:[],
            packageNoList:[],
        },
        methods:{
            packageNoIsChecked(packageNo){
                return this.detail.package_nos.indexOf(packageNo)!==-1
            },
            showPackagelList(channelId){
                layui.use(['form','jquery'], function () {
                    var $ = layui.$;
                    $.get('/admin/gameChannel/'+channelId,{},function (res) {
                        var nos = [];
                        res.data.packages.forEach(item=>{
                            nos.push(item.package_no)
                        })
                        _this.packageNoList = nos
                    })
                })
            }
        },
        created(){
            _this = this
        },
        updated:function(){
            form.render();
        },
        mounted:function(){
            layui.use(['form','jquery'], function () {
                form = layui.form;
                var layer = layui.layer
                $ = layui.$;
                if (Id){ //编辑操作
                    $.get('/admin/versionEvent/'+Id,{},function(res){
                        _this.detail = res.data;
                        selectedChannelId = res.data.channel_id;
                        _this.showPackagelList(res.data.channel_id);
                    });
                }
                $.get('/admin/gameChannel/paginate',{},function(res){
                    _this.gameChannelList  = res.data;
                });
                //监听提交
                form.on('submit(submit)', function (data) {
                    var formData = data.field;
                    if (Id){ //eidt
                        formData._method = 'put';
                        $.post('/admin/versionEvent/'+Id,formData,function(res){
                            if (res.code===0){
                                layer.msg('success',{icon:6,time:500},function(){
                                    parent.location.reload();//父页面刷新
                                })
                            } else {
                                layer.msg(res.msg,{icon:5})
                            }
                        })
                    } else { //add
                        $.post('/admin/versionEvent/add',formData,function(res){
                            if (res.code===0){
                                layer.msg('success',{icon:6,time:500},function(){
                                    parent.location.reload();//父页面刷新
                                })
                            } else {
                                layer.msg(res.msg,{icon:5})
                            }
                        })
                    }

                    return false;
                });
                form.on("select(channel_id)",function(data){
                    var channelId = data.value;
                    selectedChannelId = channelId;
                    _this.showPackagelList(channelId);
                })
                form.on("radio(package_model)",function(data){
                    var value = data.value;
                    _this.detail.package_model = value
                    if(value==2){ //全部包
                        _this.packageNoList = []
                    } else {
                        _this.showPackagelList(selectedChannelId);
                    }
                })
                form.render();
            });
        }
    })
</script>

</body>
</html>